/**
 * Created by zqk on 2018/5/4.
 */
var currentCaseId = 0;
var currentTestId = 0;
var testList;
var caseList;
var projectId=$('#projectId').val();
$(document).ready(function () {
    var form=new xhForm($('#xh_form'));
    $('.xh-datagrid-toolbar').parseToolbar();
   // 设置禁用
    $('#bt_edit').setButtonDisable('eu');//任务只有选中才能编辑
    $('#bt_del').setButtonDisable('eu');//任务只有选中才能删除
    $('#bt_list_delete').setButtonDisable('eu');
    $('#bt_list_add').setButtonEnable('eu');
    $('#bt_list_edit').setButtonDisable('eu');
    $('#bt_list_link').setButtonDisable('eu');
    form.parse();
    form.bindRequired($('#bt_form_add,#bt_form_edit'));//绑定事件
    caseList = $('#caseList');
    //case
    $btn_menu = $('#bt_state').menubutton({
        iconCls: 'fa fa-bars',
        menu: '#testCaseResult',
    });
    //开启或者完成任务
    $($btn_menu.menubutton('options').menu).menu({
        onClick:function (item) {
            //item 的相关属性参见API中的menu
            var iActualResult
            if(item.text == '成功'){
                iActualResult = 2;
            }else if(item.text == '失败'){
                iActualResult = 3;
            }
            if(caseList.datagrid('getSelections').length == 0){
                window.top.xhTop.showError('错误','请选择一个测试用例');
                return false;
            }
            window.top.swal({
                showCancelButton: true,
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                showLoaderOnConfirm: true,
                allowOutsideClick: false,
                title: '备注',
                input: 'textarea',
                preConfirm: function(remarks) {
                    return new Promise(function (resolve, reject) {
                        if(!remarks){
                            reject('备注不能为空！');
                        }
                        else {
                            $.ajax({
                                url :moduleUrl+'test/changeStatus',
                                data:{iId:currentCaseId,iActualResult:iActualResult,iProjectId:projectId,testId:currentTestId,remarks:remarks},
                                type:'POST',
                                success:function (data) {
                                    window.top.xhTop.showSuccess(
                                        data.msg
                                    );
                                    caseList.datagrid('reload');
                                },
                                error:function (data) {
                                    window.top.xhTop.showError(
                                        data.responseJSON.error,
                                        data.responseJSON.msg
                                    );
                                }
                            });
                        }
                    })
                }
            }).catch(swal.noop);
        }
    });
    caseList.datagrid({
        url: moduleUrl+'test/getCases',
        method: 'post',
        queryParams:{
            iProjectId:projectId,
            iTestId:currentTestId,
        },
        rownumbers: true,
        pagination: true,
        pageSize: 50,
        pageList: [20,50,100,200],
        idField: 'iId',
        singleSelect:true,
        checkOnSelect: true,
        selectOnCheck: true,
        frozenColumns:[[
            {field:'ck',checkbox:true},
            {field:'iId',title:'iId',hidden:true},
            {field:'sCode',title:'编号',width:140,halign:'center',align:'left'},
        ]],
        columns:[[
            {field:'sModule',title:'模块',width:110,halign:'center',align:'left'},
            {field:'sSubmodule',title:'子模块',width:110,halign:'center',align:'left'},
            {field:'sFunctionPoint',title:'功能点',width:140,halign:'center',align:'left'},
            {field:'sPrecondition',title:'前置条件',width:100,halign:'center',align:'left'},
            {field:'sSteps',title:'步骤',width:150,halign:'center',align:'left'},
            {field:'iLevel',title:'等级',width:80,align:'center',halign:'center',
                formatter: function(value,row,index){
                    if(value =='1'){
                        var str = '<div style = "background-color:#CCCCCC;color: #ffffff;height: 20px; line-height: 20px;">'+levels[value]+'</div>'
                        return str;
                    }else if(value =='2'){
                        var str = '<div style = "background-color:#EEEE00;color: #ffffff;height: 20px; line-height: 20px;">'+levels[value]+'</div>'
                        return str;
                    }else if(value =='3'){
                        var str = '<div style = "background-color:#ff1200;color: #ffffff;height: 20px; line-height: 20px;">'+levels[value]+'</div>'
                        return str;
                    }
                }
            },
            {field:'sExpectedResult',title:'预期结果',width:120,halign:'center',align:'left'},
            {field:'iActualResult',title:'测试结果',width:100,align:'center',halign:'center',
                formatter: function(value,row,index){
                    if(value =='1'){
                        var str = '<div style = "background-color:#CCCCCC;color: #ffffff;height: 20px; line-height: 20px;">'+results[value]+'</div>'
                        return str;
                    }else if(value =='2'){
                        var str = '<div style = "background-color:#0079c8;color: #ffffff;height: 20px; line-height: 20px;">'+results[value]+'</div>'
                        return str;
                    }else if(value =='3'){
                        var str = '<div style = "background-color:#ff1b00;color: #ffffff;height: 20px; line-height: 20px;">'+results[value]+'</div>'
                        return str;
                    }
                }
            },
            {field:'sDefectNumber',title:'缺陷编号',width:100,align:'right',halign:'center'},
            {field:'sRemarks',title:'备注',width:100,align:'right',halign:'center'},
            {field:'tCreateDate',title:'创建时间',width:125,halign:'center',align:'center'},
            {field:'tLastModifyDate',title:'最后编辑时间',width:125,halign:'center',align:'center'},
        ]],
        onCheck:function (rowIndex, rowData) {
            if(currentCaseId==rowData['iId']){
                currentCaseId = 0;
                caseList.datagrid('unselectRow',rowIndex);
                $('#bt_edit').setButtonDisable('eu');
                $('#bt_del').setButtonDisable('eu');
            }else{
                currentCaseId = rowData['iId'];
                $('#bt_edit').setButtonEnable('eu');//编辑enable
                $('#bt_del').setButtonEnable('eu');//删除enable
            }
        }
    });

    // $('#bt_list_search').click(function () {
    //     var status=$('#taskstatus').val();
    //     var level=$('#tasklevel').val();
    //     var userid=$('#memberid').val();
    //     taskList.treegrid('load',{status:status,level:level,userid:userid,projectid:projectid,selectProjectId:selectProjectId});
    // });

    $('#caseList_toolbar a').click(function (){
        var eleid = $(this).attr('id');
        $.ajax({
            url:moduleUrl+'test/checkTestListPower',
            data:{iProjectId:projectId,iTestId:currentTestId},
            type:'POST',
            success:function (data) {
                var obj=caseList.datagrid('getSelections')[0];
                if(eleid=='bt_edit'){
                    if(!obj) return false;
                    window.top.xhTop.showModal({
                        title:'编辑测试用例',
                        content: '<iframe src="'+moduleUrl+'test/caseForm?iId='+obj['iId']+'"  style="width: 100%;border-style: none;padding: 0px;height: 400px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        backClose: false,
                    });
                }else if(eleid =='bt_del'){
                    if(!obj) return false;
                    myWindow.initPopUp({title: '确认删除', text: '确定要删除吗?'},function () {
                        $.ajax({
                            url:moduleUrl+'test/delCaseById',
                            data:{iId:obj['iId']},
                            type:'POST',
                            success:function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                caseList.datagrid('reload');
                            },
                            error:function (data) {
                                window.top.xhTop.showError(
                                    data.responseJSON.error,
                                    data.responseJSON.msg
                                )
                            }
                        })
                    });
                }else if(eleid =='bt_list_link'){
                    window.top.xhTop.showModal({
                        title:'添加测试用例',
                        width:660,
                        content: '<iframe src="'+moduleUrl+'test/productTestCases?iProjectId='+projectId+'&iTestId='+currentTestId+'"  style="width: 100%;border-style: none;padding: 0px;height: 400px"  scrolling="yes"   ></iframe>',
                        backClose: false
                    });

                }
            },
            error:function (data) {
                window.top.xhTop.showError(
                    data.responseJSON.error,
                    data.responseJSON.msg
                );
            }
        });
    });

    //test
    //--------------------------------------------------华丽分割线-------------------------------------------------------
    testList = $('#testList');
    testList.datagrid({
        url: moduleUrl+'test/getTests',
        method: 'post',
        queryParams:{
            iProjectId:projectId,
        },
        rownumbers: true,
        pagination: true,
        pageSize: 50,
        pageList: [20,50,100,200],
        idField: 'iId',
        singleSelect:true,
        checkOnSelect: true,
        selectOnCheck: true,
        columns:[[
            {field:'ck',checkbox:true},
            {field:'iId',title:'iId',hidden:true},
            {field:'sName',title:'名称',width:240,align:'left'},
        ]],
        onCheck:function (rowIndex, rowData) {
            if(currentTestId == rowData['iId']){
                currentTestId = 0;
                testList.datagrid('unselectRow',rowIndex);
                $('#bt_list_edit').setButtonDisable('eu');//编辑enable
                $('#bt_list_link').setButtonDisable('eu');//编辑enable
                $('#bt_list_delete').setButtonDisable('eu');//删除enable
                console.log(testList.datagrid('getSelections'));
            }else{
                currentTestId = rowData['iId'];
                $('#bt_list_edit').setButtonEnable('eu');//编辑enable
                $('#bt_list_link').setButtonEnable('eu');//编辑enable
                $('#bt_list_delete').setButtonEnable('eu');//删除enable
            }
            caseList.datagrid('reload',{iTestId:currentTestId,iProjectId:projectId});
        },
    });

    $('#testList_toolbar a').click(function () {
        var eleid = $(this).attr('id');
        var obj = testList.datagrid('getSelections')[0];
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }else if(eleid =='bt_list_delete'&&obj!=undefined){
            myWindow.initPopUp({title: '确认删除', text: '确定要删除吗?'},function () {
                $.ajax({
                    url:moduleUrl+'test/delTestById',
                    data:{iTestId:obj['iId']},
                    type:'POST',
                    success:function (data) {
                        currentTestId = 0;
                        window.top.xhTop.showSuccess(
                            data.msg
                        );
                        testList.datagrid('clearSelections');
                        testList.datagrid('reload');
                        // caseList.datagrid(reload);
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        }else if(eleid =='bt_list_edit'&&obj!=undefined){
            window.top.swal({
                showCancelButton: true,
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                showLoaderOnConfirm: true,
                allowOutsideClick: false,
                title: '编辑项目测试',
                input: 'text',
                inputValue:obj['sName'],
                preConfirm: function(sName) {
                    return new Promise(function (resolve,reject) {
                        if(!sName){
                            reject('名称不能为空！');
                            return false;
                        }
                        $.ajax({
                            url: moduleUrl + 'test/saveTest',
                            data:{iTestId:obj['iId'],iProjectId:projectId,sName:sName},
                            type: 'POST',
                            success: function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                testList.datagrid('reload');
                            },
                            error: function (data) {
                                window.top.xhTop.showError(
                                    data.responseJSON.error,
                                    data.responseJSON.msg
                                );
                            }
                        });
                    });
                }
            });

        } else if(eleid =='bt_list_add'){
            window.top.swal({
                showCancelButton: true,
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                showLoaderOnConfirm: true,
                allowOutsideClick: false,
                title: '新增项目测试',
                input: 'text',
                preConfirm: function(sName) {
                    return new Promise(function (resolve,reject) {
                        if(!sName){
                            reject('名称不能为空！');
                            return false;
                        }
                        $.ajax({
                            url: moduleUrl + 'test/saveTest',
                            data:{iProjectId:projectId,sName:sName},
                            type: 'POST',
                            success: function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                testList.datagrid('reload');
                            },
                            error: function (data) {
                                 window.top.xhTop.showError(
                                     data.responseJSON.error,
                                     data.responseJSON.msg
                                 );
                            }
                        });
                    });
                }
            });
        }
    });

});

